package com.redisdemo.job;

import com.redisdemo.common.RedisConstant;
import com.redisdemo.domain.Message;
import com.redisdemo.utlis.RedisUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * <h3>redisDemo</h3>
 * <p>消息任务</p>
 *
 * @author gdw
 * @since 2024-06-25 16:22:11
 */
public class MessageJob  implements Job {
    @Autowired
    private RedisUtils redisUtils;

    public ConcurrentLinkedQueue<Message> messages=new ConcurrentLinkedQueue<>();
    /**
     * 定时任务执行器:将redis中的消息放入到ConcurrentLinkedQueue中
     * @param context
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            Message message = (Message) redisUtils.lGetRight(RedisConstant.REDIS_LIST_MESSAGE);
            if (message != null) {
                // messages.add(message);
                System.out.println(message.getReceiveName() + " 接收到用户:" +message.getSendName() + " 发送的:"+message.getMessage()+"消息");
            }
        } catch (Exception e) {
            // 捕获并记录异常
            System.err.println("Error executing job: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
